package com.jhs.boot.system.service.impl;

import com.jhs.boot.system.model.vo.NodesThirdVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhs.boot.system.mapper.SysNodesDhcpMapper;
import com.jhs.boot.system.service.SysNodesDhcpService;
import com.jhs.boot.system.model.entity.SysNodesDhcp;
import com.jhs.boot.system.model.form.SysNodesDhcpForm;
import com.jhs.boot.system.model.query.SysNodesDhcpQuery;
import com.jhs.boot.system.model.vo.SysNodesDhcpVO;
import com.jhs.boot.system.converter.SysNodesDhcpConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 节点IP匹配服务实现类
 *
 * @author youlaitech
 * @since 2025-10-29 12:50
 */
@Service
@RequiredArgsConstructor
public class SysNodesDhcpServiceImpl extends ServiceImpl<SysNodesDhcpMapper, SysNodesDhcp> implements SysNodesDhcpService {

    private final SysNodesDhcpConverter sysNodesDhcpConverter;

    /**
    * 获取节点IP匹配分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SysNodesDhcpVO>} 节点IP匹配分页列表
    */
    @Override
    public IPage<SysNodesDhcpVO> getSysNodesDhcpPage(SysNodesDhcpQuery queryParams) {
        Page<SysNodesDhcpVO> pageVO = this.baseMapper.getSysNodesDhcpPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取节点IP匹配表单数据
     *
     * @param id 节点IP匹配ID
     * @return 节点IP匹配表单数据
     */
    @Override
    public SysNodesDhcpForm getSysNodesDhcpFormData(Long id) {
        SysNodesDhcp entity = this.getById(id);
        return sysNodesDhcpConverter.toForm(entity);
    }
    
    /**
     * 新增节点IP匹配
     *
     * @param formData 节点IP匹配表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveSysNodesDhcp(SysNodesDhcpForm formData) {
        SysNodesDhcp entity = sysNodesDhcpConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新节点IP匹配
     *
     * @param id   节点IP匹配ID
     * @param formData 节点IP匹配表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateSysNodesDhcp(Long id,SysNodesDhcpForm formData) {
        SysNodesDhcp entity = sysNodesDhcpConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除节点IP匹配
     *
     * @param ids 节点IP匹配ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSysNodesDhcps(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的节点IP匹配数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public boolean saveDhcp( NodesThirdVO  nodesBean) {
        SysNodesDhcp entity = sysNodesDhcpConverter.toNodesThirdVO(nodesBean);
        return this.save(entity);
    }
}
